﻿using Microsoft.Xna.Framework;
using OilRigSumoWrestlers.Controllables.Controllers;
using OilRigSumoWrestlers.Controllables.GameModes;
using OilRigSumoWrestlers.Entities;
using OilRigSumoWrestlers.Levels;
using OilRigSumoWrestlers.NetworkTools;
using RaptorEngine;
using RaptorEngine.Managers;

namespace OilRigSumoWrestlers.Scenes.Network
{
    class NetworkGame : Scene
    {
        private PlayerCharacter _player1;
        private PlayerCharacter _player2;

        public NetworkGame(bool network) : base(network)
        {
            Network = network;
            Camera = new Camera(Vector3.Forward, new Vector3(0, 200, 100));
            CreateScene();
            ((SumoNetwork)GameEngine.Instance.Network.GameNetwork).SetGamers(_player1, _player2);
            GameEngine.Instance.Network.State = NetworkManager.NetworkState.Playing;
            GameEngine.Instance.Network.SwitchState();
            GameEngine.Instance.Audio.AddSong("breakdown", "Sounds/breakdown");
            GameEngine.Instance.Audio.StartSong = true;
            GameEngine.Instance.Audio.AddSong("win", "Sounds/win");
            GameEngine.Instance.Audio.PlayMusic("breakdown", true);
        }

        private void CreateScene()
        {
            // Camera
            Camera = new Camera(
                new Vector3(
                    ((GameConstant.LVL_LEVELSIZE - 1) / 2) * -GameConstant.LVL_TILESIZE,
                    0,
                    ((GameConstant.LVL_LEVELSIZE - 1) / 2) * GameConstant.LVL_TILESIZE),
                new Vector3(
                    (-((GameConstant.LVL_LEVELSIZE / 2) * GameConstant.LVL_TILESIZE) - GameConstant.LVL_TILESIZE * 5),
                    (GameConstant.LVL_LEVELSIZE * GameConstant.LVL_TILESIZE) + 2 * GameConstant.LVL_TILESIZE,
                    (((GameConstant.LVL_LEVELSIZE - 1) / 2) * GameConstant.LVL_TILESIZE)));

            // Player 1
            _player1 = new PlayerCharacter("Models/Characters/player1");
            _player1.Position = new Vector3(20, 20, 10);
            if (GameEngine.Instance.Network.CheckHost())
            {
                _player1.AddComponent(new NetworkControllable(_player1, LogicalPlayerIndex.One));
                _player1.Networkable = true;
            }
            else
            {
                _player1.AddComponent(new NetworkOpponentControllable(_player1));
            }

            // Player 2
            _player2 = new PlayerCharacter("Models/Characters/player2");
            _player2.Position = new Vector3(20, 20, 30);
            if (!GameEngine.Instance.Network.CheckHost())
            {
                _player2.AddComponent(new NetworkControllable(_player2, LogicalPlayerIndex.One));
                _player2.Networkable = true;
            }
            else
            {
                _player2.AddComponent(new NetworkOpponentControllable(_player2));
            }

            // Level design
            Level2 level = new Level2();
            _player1.Position = level.GetPlayer1StartPosition();
            _player2.Position = level.GetPlayer2StartPosition();

            // Game Mode
            Entity gamemode = new Entity();
            NetworkKnockoutMode nko = new NetworkKnockoutMode(gamemode, _player1, _player2, -20f, 3, level);
            gamemode.AddComponent(nko);

            AddEntity(_player1);
            AddEntity(_player2);

            //Level structure
            foreach (Entity tile in level.GetAllTiles())
            {
                AddEntity(tile);
            }

            //GameMode
            AddEntity(gamemode);
        }
    }
}
